##########################################################################
##########################################################################
########### Racial Resentment vs. Principled Conservatism ################
##########################################################################
##########################################################################

library(stats)
library(lattice)
library(foreign)

##########################################################################

# Clean ANES data
anes2016 <- read.dta("Clean Data, 2016.dta")
anes2004 <- read.dta("Clean Data, 2004.dta")
anes1992 <- read.dta("Clean Data, 1992.dta")

##########################################################################

#####
## Relationship between racial
## resentment and ideology measures
#####

ideocor <- read.csv("Corr with Ideo.csv", header = TRUE)

xyplot(estimate ~ year,
       data = ideocor,
       aspect = 0.75,
       group = ideocor$item,
       ylim = c(0.01, 0.68),
       xlim = c(1988, 2020),
       pch = c(16, 17),
       col = "black",
       xlab = "Year",
       ylab = "Correlation w/ Self-Identification",
       scales = list(x = list(at = c(1992, 2004, 2016))),
       key=list(columns=1,points=list(pch=c(17,16)), 
                text=list(c("Spending Preferences", "Size/Scope of Government"))),
       panel = panel.superpose,
       panel.groups = function(x, y, subscripts, groups, col, lty, ...) {
         panel.xyplot(x, y, pch =...)
         panel.segments(x, ideocor$lower[subscripts], 
                        x, ideocor$upper[subscripts], lty = 1, col = "black")
       }
)

##########################################################################

#####
## Relationship between racial
## resentment and ideology measures
#####

rrcor <- read.csv("Corr with RR.csv", header = TRUE)

xyplot(estimate ~ year,
       data = rrcor,
       aspect = 0.75,
       group = rrcor$item,
       ylim = c(-0.12, 0.68),
       xlim = c(1988, 2020),
       pch = c(16, 17, 15),
       col = "black",
       xlab = "Year",
       ylab = "Correlation w/ Racial Resentment",
       scales = list(x = list(at = c(1992, 2004, 2016))),
       key=list(columns=1,points=list(pch=c(15, 17,16)), 
                text=list(c("Spending Preferences", "Size/Scope of Government", "Self-Identification"))),
       panel = panel.superpose,
       panel.groups = function(x, y, subscripts, groups, col, lty, ...) {
         panel.xyplot(x, y, pch =...)
         panel.segments(x, rrcor$lower[subscripts], 
                        x, rrcor$upper[subscripts], lty = 1, col = "black")
       }
)

##########################################################################

##### 
## Histogram differences in 
## racial resentment discrimination
## and difficulty parameter estimates 
## over time and across ideology 
## measures
#####

diff <- read.csv("Discrimination Diff.csv", header = TRUE)

histogram(~diff,
          data = diff[which(diff$year>2004),],
          xlab = "Difference in Estimates",
          main = "A. Discrimination Parameters"
          )

diff2 <- read.csv("Difficulty Diff.csv", header = TRUE)

histogram(~diff2,
          data = diff2[which(diff2$year>2004),],
          xlab = "Difference in Estimates",
          main = "B. Difficulty Parameters"
)

##########################################################################

##### 
## Histograms of ideology and 
## racial resentment scales 
#####

# 2016 ANES
histogram(~as.factor(ideo),
          data = anes2016,
          main = "A. Ideological Self-Identification",
          xlab = "",
          aspect = 1,
)

histogram(~spending,
        data = anes2016,
        main = "B. Spending Preferences",
        xlab = "",
        aspect = 1,
)

histogram(~as.factor(biggov),
                   data = anes2016,
                   main = "C. Government Scope/Size",
                   xlab = "",
                   aspect = 1,
)

# 2004 ANES
histogram(~as.factor(ideo),
          data = anes2004,
          main = "A. Ideological Self-Identification",
          xlab = "",
          aspect = 1,
)

histogram(~spending,
          data = anes2004,
          main = "B. Spending Preferences",
          xlab = "",
          aspect = 1,
)

histogram(~as.factor(biggov),
          data = anes2004,
          main = "C. Government Scope/Size",
          xlab = "",
          aspect = 1,
)


# 1992 ANES
histogram(~as.factor(ideo),
          data = anes1992,
          main = "A. Ideological Self-Identification",
          xlab = "",
          aspect = 1,
)

histogram(~spending,
          data = anes1992,
          main = "B. Spending Preferences",
          xlab = "",
          aspect = 1,
)

histogram(~as.factor(biggov),
          data = anes1992,
          main = "C. Government Scope/Size",
          xlab = "",
          aspect = 1,
)


##########################################################################

##### 
## Effects plots 
#####

# Aid to minorities
aid <- read.csv("Aid to Minorities.csv", header = TRUE)
head(aid)

aid$cond[aid$difvar=="Self-Identification"] <- 1
aid$cond[aid$difvar=="Spending Prefs."] <- 3
aid$cond[aid$difvar=="Size/Scope of Gov."] <- 2

aid$variable <- factor(aid$variable,levels(aid$variable)[c(1, 4, 5, 3, 2)])

xyplot(ynum ~ estimate | as.factor(difvar), 
       data = aid,
       aspect = 1.5,
       groups = equation,
       scond = aid$cond,
       #ylim = c(0, 75),
       xlim = c(-0.1, .75),
       col = "black",
       pch = c(16,17),
       ylab = "",       
       xlab = "OLS Estimate",
       scales=list(y=list(labels=c("Party ID","Party ID", "Size/Scope Beliefs", "Spending Prefs.",
                         "Self-Identification", "Racial Resentment"))),
       key=list(columns=1,points=list(pch=c(16,17)), 
                text=list(c("Uncorrected", "Corrected"))),
       panel = panel.superpose,
       panel.groups = function(x, y, subscripts, groups, col, lty, scond, ...) {
         pnl = panel.number()
         panel.xyplot(x, y, pch=...)
         panel.segments(x0=aid$lower[scond==pnl], y0=aid$ynum[scond==pnl],
                        x1=aid$upper[scond==pnl], y1=aid$ynum[scond==pnl], col = "black")
         panel.abline(v=0, lty=2, col="dark grey")
       }
)


# Preferential hiring for blacks
pref <- read.csv("Pref Hire.csv", header = TRUE)
head(pref)

pref$cond[pref$difvar=="Self-Identification"] <- 1
pref$cond[pref$difvar=="Spending Prefs."] <- 3
pref$cond[pref$difvar=="Size/Scope of Gov."] <- 2

pref$variable <- factor(pref$variable,levels(pref$variable)[c(1, 4, 5, 3, 2)])

xyplot(ynum ~ estimate | as.factor(difvar), 
       data = pref,
       aspect = 1.5,
       groups = equation,
       scond = pref$cond,
       #ylim = c(0, 75),
       xlim = c(-0.1, .75),
       col = "black",
       pch = c(16,17),
       ylab = "",       
       xlab = "OLS Estimate",
       key=list(columns=1,points=list(pch=c(16,17)), 
                text=list(c("Uncorrected", "Corrected"))),
       scales=list(y=list(labels=c("Party ID","Party ID", "Size/Scope Beliefs", "Spending Prefs.",
                                   "Self-Identification", "Racial Resentment"))),
       panel = panel.superpose,
       panel.groups = function(x, y, subscripts, groups, col, lty, scond, ...) {
         pnl = panel.number()
         panel.xyplot(x, y, pch=...)
         panel.segments(x0=pref$lower[scond==pnl], y0=pref$ynum[scond==pnl],
                        x1=pref$upper[scond==pnl], y1=pref$ynum[scond==pnl], col = "black")
         panel.abline(v=0, lty=2, col="dark grey")
       }
)


# Healthcare
health <- read.csv("Healthcare.csv", header = TRUE)
head(health)

health$cond[health$difvar=="Self-Identification"] <- 1
health$cond[health$difvar=="Spending Prefs."] <- 3
health$cond[health$difvar=="Size/Scope of Gov."] <- 2

health$variable <- factor(health$variable,levels(health$variable)[c(1, 4, 5, 3, 2)])

xyplot(ynum ~ estimate | as.factor(difvar), 
       data = health,
       aspect = 1.5,
       groups = equation,
       scond = health$cond,
       #ylim = c(0, 75),
       xlim = c(-0.1, .75),
       col = "black",
       pch = c(16,17),
       ylab = "",       
       xlab = "OLS Estimate",
       key=list(columns=1,points=list(pch=c(16,17)), 
                text=list(c("Uncorrected", "Corrected"))),
       scales=list(y=list(labels=c("Party ID","Party ID", "Size/Scope Beliefs", "Spending Prefs.",
                                   "Self-Identification", "Racial Resentment"))),
       panel = panel.superpose,
       panel.groups = function(x, y, subscripts, groups, col, lty, scond, ...) {
         pnl = panel.number()
         panel.xyplot(x, y, pch=...)
         panel.segments(x0=health$lower[scond==pnl], y0=health$ynum[scond==pnl],
                        x1=health$upper[scond==pnl], y1=health$ynum[scond==pnl], col = "black")
         panel.abline(v=0, lty=2, col="dark grey")
       }
)

